package com.epalmpay.service.thread;

import com.epalmpay.entity.ActivityBase;
import com.epalmpay.enumdef.ActivityEnum;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.mapper.ActivityBaseMapper;
import com.epalmpay.service.groupaccount.IGroupAccountService;

public class AfterRebackMoneyThread implements Runnable {
    private Long groupId;
    private Long activityId;
    private Integer backAmount;
    private Integer bonusAmount;
    private ActivityBaseMapper activityBaseMapper;
    private IGroupAccountService groupAccountService;

    public AfterRebackMoneyThread(Long activityId, Integer backAmount, ActivityBaseMapper activityBaseMapper,
                                  IGroupAccountService groupAccountService, Integer bonusAmount, Long groupId) {
        this.groupId = groupId;
        this.activityId = activityId;
        this.backAmount = backAmount;
        this.activityBaseMapper = activityBaseMapper;
        this.groupAccountService = groupAccountService;
        this.bonusAmount = bonusAmount;
    }

    @Override
    public void run() {
        //检查


        //营销账户余额扣除
        try {
            groupAccountService.updateAmount(groupId, BizEnum.SaleAccountSubjectType.ConsumeBackCashGet,
                    backAmount, null, activityId, null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(bonusAmount != null) {
            backAmount = bonusAmount;
        }

        activityBaseMapper.minusMaxAmountById(activityId, backAmount);

        ActivityBase activityBase = activityBaseMapper.selectByPrimaryKey(activityId);
        if(activityBase.getUsedAmount() + backAmount >= activityBase.getMaxAmount()) {
            activityBase.setStatus(ActivityEnum.ActivityStatus.Over.getType());
            activityBaseMapper.updateByPrimaryKeySelective(activityBase);
        }
    }
}
